<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <script th:src="@{/webjars/jquery/3.5.1/jquery.min.js}"></script>
    <script th:src="@{/js/ions_alert.js}"></script>
    <script th:src="@{/common/common.js}"></script>
    <script th:src="@{/js/sweetalert.min.js}"></script>
    <link rel="stylesheet" th:href="@{/css/sweetalert.css}">
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <dd class="layui-form layui-col-md12 x-so">
            <input class="layui-input" placeholder="商品码" name="no" id="no">
            <input class="layui-input" placeholder="商品名" name="bookName" id="bookName" >
            <div class="layui-input-inline">
                <select name="controller" id="status">
                    <option>--全部状态--</option>
                    <option>正常</option>
                    <option>预警</option>
                </select>
            </div>

            <input value="" type="text" name="username" placeholder="请输入修改人" autocomplete="off"
                   class="layui-input" id="modifyMan">
            <button class="layui-btn" lay-submit="" lay-filter="search" id="search"><i class="layui-icon" >&#xe615;</i></button>
        </dd>
    </div>
    <xblock>
<!--        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>-->
        <button class="layui-btn" onclick="x_admin_show('添加商品','wares_add',600,700)"><i
            class="layui-icon"></i>添加
        </button>
        <span class="x-right" style="line-height:40px">共有数据：<span id ="many" style="color: #bf1707" th:text="${WARE_PAGE_LIST.getTotalCount()}"></span>条</span>
    </xblock>
    <table class="layui-table middle">
        <thead>
        <tr>
            <th width="6%">
                <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i
                        class="layui-icon">&#xe605;</i></div>
            </th>
            <th>商品码</th>
            <th>商品名</th>
            <th>商品数量</th>
            <th>进价</th>
            <th>售价</th>
            <th>积分</th>
            <th>状态</th>
            <th>修改者</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tbody" class="waresList">
        <tr th:each="wares:${WARE_PAGE_LIST.getList()}" id="wares">
            <td>
                <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='2'><i
                        class="layui-icon">&#xe605;</i></div>
            </td>
            <td id="id" th:text="${wares.getNo()}"></td>
            <td id="name" th:text="${wares.getName()}"></td>
            <td id="number" th:text="${wares.getNumber()}">{.Pid}}</td>
            <td id="in_price" th:text="${wares.getTradePrice()}">{{.Desc}}</td>
            <td id="out_price" th:text="${wares.getRetailPrice()}">{{date .CreateTime "Y-m-d H:m:s"}}</td>
            <td id="point" th:text="${wares.getPoints()}"></td>
            <td class="td-status" th:if="${wares.getWaresStatus() == '正常'}">
                <span class="layui-btn layui-btn-normal layui-btn-mini" th:if="${wares.getWaresStatus() == '正常'}">正常</span>
            </td>
            <td class="td-status" th:if="${wares.getWaresStatus() == '预警'}">
                <span class="layui-btn layui-btn-danger layui-btn-mini" >预警</span>
            </td>
            <td id="modify" th:text="${wares.getLastArchivist()}"></td>
            <td class="td-manage">
                <a th:onclick="statusGo([[${wares.getNo()}]],[[${wares.getWaresStatus()}]])" href="javascript:"
                   class="layui-btn layui-btn-mini layui-btn-danger is_active_btn">
                    <i class="layui-icon">&#xe601;</i>预警
                </a>
                <a href="javascript:" th:onclick="x_admin_show('编辑商品','waresEdit?no='+[[${wares.getNo()}]],600,500)"
                    style="cursor: pointer" class="layui-btn layui-btn-mini layui-btn-normal">
                    <i class="layui-icon">&#xe642;</i>编辑
                </a>
                <a style="cursor: pointer" class="layui-btn layui-btn-mini layui-btn-danger delete_btn" th:onclick="deleteConfirm([[${wares.getNo()}]])">
                    <i class="layui-icon" >&#x1006;</i>下架
                </a>
            </td>
        </tr>
        </tbody>
    </table>
    <div class="page" id="page">
        <div>
            <span class="current" id="curr" th:text="${'共'+WARE_PAGE_LIST.getTotalPage()+'页'}"></span>
<!--            <a class="prev" href="{{urlfor "AuthController.List"}}?page={{.pageMap.firstPage}}&keyword={{.keyword}}"><&lt;</a>-->
            <a class="prev" href="javaScript:" id="begin" onclick="pageGo('begin')"><&lt;</a>
            <a class="prev" href="javaScript:" onclick="pageGo('forward')">&lt;</a>
            <span class="current" id="currentPage" th:text="${WARE_PAGE_LIST.getCurrentPage()+1}"></span>
<!--            <a class="next" href="#" >啊</a>-->
            <a class="next" href="javaScript:" onclick="pageGo('nextPage')">&gt;</a>
            <a class="next" href="javaScript:" onclick="pageGo('end')">&gt;&gt;</a>
        </div>
    </div>

</div>
<script>

    $(function () {
        $("#search").click(function () {
            let no = $("#no").val();
            let bookName = $("#bookName").val();
            let modifyMan = $("#modifyMan").val();
            let currentPage = $("#currentPage").text();
            let curr = $("#curr").text();
            let status = $("#status").val();

            // console.log(status);
            findPage(no,bookName,modifyMan,'search',currentPage,curr,status);
        })
    });

    function statusGo(id,status) {

        if(status === '预警'){
            alert("请勿重复添加");
        }else {
            $.ajax({
                url: "waresStatusChange",
                type: "POST",
                data: {
                    no:id,
                    status:'预警'
                },
                success:function (resultInfo) {
                    if(resultInfo.flag){
                        alert(resultInfo.message);
                        window.location.reload();
                    }else {
                        alert(resultInfo.message);
                    }
                }
            });
        }
    }
    function pageGo(param){
        let no = $("#no").val();
        let bookName = $("#bookName").val();
        let modifyMan = $("#modifyMan").val();
        let currentPage = $("#currentPage").text();
        let curr = $("#curr").text();
        let status = $("#status").val();
        findPage(no,bookName,modifyMan,param,currentPage,curr,status);
    }
    function findPage(no,bookName,modifyMan,param,currentPage,curr,status) {
        $.ajax({
            url: "pageWaresQuery",
            type: "PUT",
            data:　{
                no:no,
                bookName:bookName,
                modifyMan:modifyMan,
                param: param,
                currentPage: currentPage,
                curr:curr,
                status:status
            },
            success:function (data) {
                var text = '';

                for (let i = 0; i < data.data.list.length; i++) {
                    text += '<tr th:each="wares:${WARE_PAGE_LIST.getList()}" id="wares">\n' +
                        '            <td>\n' +
                        '                <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id=\'2\'><i\n' +
                        '                        class="layui-icon">&#xe605;</i></div>\n' +
                        '            </td>\n' +
                        '            <td id="id" th:text="${wares.getNo()}">'+data.data.list[i].no+'</td>\n' +
                        '            <td id="name" th:text="${wares.getName()}">'+data.data.list[i].name+'</td>\n' +
                        '            <td id="number" th:text="${wares.getNumber()}">'+data.data.list[i].number+'</td>\n' +
                        '            <td id="in_price" th:text="${wares.getTradePrice()}">'+data.data.list[i].tradePrice+'</td>\n' +
                        '            <td id="out_price" th:text="${wares.getRetailPrice()}">'+data.data.list[i].retailPrice+'</td>\n' +
                        '            <td id="point" th:text="${wares.getPoints()}">'+data.data.list[i].points+'</td>\n';

                    if(data.data.list[i].waresStatus === '正常'){
                        text +=  '            <td class="td-status">\n' +
                            '                <span class="layui-btn layui-btn-normal layui-btn-mini" >正常</span>\n' +
                            '            </td>\n';
                    }
                    else if(data.data.list[i].waresStatus === '预警') {
                        // console.log(data.data.list[i]);
                        text +=
                            '            <td class="td-status">\n' +
                            '                <span class="layui-btn layui-btn-danger layui-btn-mini" >预警</span>\n' +
                            '            </td>\n';
                    }else {
                        text += '            <td class="td-status">\n' +
                            '                <span class="layui-btn layui-btn-danger layui-btn-mini" >正常</span>\n' +
                            '            </td>\n';
                    }
                    text +='            <td id="modify">'+data.data.list[i].lastArchivist+'</td>\n' +
                        '            <td class="td-manage">\n' +
                        '                <a href="javascript:" onclick="statusGo('+data.data.list[i].no+','+'\''+data.data.list[i].waresStatus+'\''+')" \n' +
                        '                   class="layui-btn layui-btn-mini layui-btn-danger is_active_btn">\n' +
                        '                    <i class="layui-icon">&#xe601;</i>预警\n' +
                        '                </a>\n' +
                        '                <a href="javascript:" onclick="x_admin_show(\'编辑商品\',\'waresEdit?no=\'+'+data.data.list[i].no+',600,500)"\n' +
                        '                    style="cursor: pointer" class="layui-btn layui-btn-mini layui-btn-normal">\n' +
                        '                    <i class="layui-icon">&#xe642;</i>编辑\n' +
                        '                </a>\n' +
                        '                <a style="cursor: pointer" class="layui-btn layui-btn-mini layui-btn-danger delete_btn" onclick="deleteConfirm('+data.data.list[i].no+')">\n' +
                        '                    <i class="layui-icon">&#x1006;</i>下架\n' +
                        '                </a>\n' +
                        '            </td>\n' +
                        '        </tr>';

                }
                text += '';
                $(".waresList").html(text);
                $("#currentPage").html(data.data.currentPage+1);
                $("#curr").html('共'+data.data.totalPage+'页');
                $("#many").text(data.data.totalCount);

            }
        })
    }

    function deleteConfirm(no) {
        let b = confirm("确认下架吗？");
        if(b){
            console.log(no);

            $.ajax({
                url:"/admin/wares/deleteWares",
                data:{
                    no:no
                },
                success:function (resultInfo) {
                    if(resultInfo.flag){
                        alert(resultInfo.message);
                        location.reload();
                    }else {
                        alert(resultInfo.message);
                    }
                }
            })
        }
    }

    // $(document).ready(function () {
    //     layui.use('laydate', function () {
    //         var laydate = layui.laydate;
    //
    //         //执行一个laydate实例
    //         laydate.render({
    //             elem: '#start' //指定元素
    //         });
    //
    //         //执行一个laydate实例
    //         laydate.render({
    //             elem: '#end' //指定元素
    //         });
    //     });
    // });

    // function auth_stop(id, is_active) {
    //     var msg = ""
    //     if (is_active == 0) {
    //         msg = CoformMsg.Stop
    //     } else {
    //         msg = CoformMsg.Start
    //     }
    //     layer.confirm(msg, function (index) {
    //         layer.close(layer.index)
    //         $.ajax({
    //             url: "",
    //             type: "post",
    //             data: {
    //                 "id": id,
    //                 "is_active": is_active
    //             },
    //             success: function (data) {
    //                 if (data.code == 200) {
    //                     ions_alert.alertSuccessToast(data.msg)
    //                 } else {
    //                     ions_alert.alertErrorToast(data.msg)
    //                 }
    //                 setTimeout(function () {
    //                     window.location.reload()
    //                 }, 1000)
    //             },
    //             error: function (data) {
    //                 ions_alert.alertErrorToast(data.msg)
    //                 setTimeout(x_admin_close, 2000)
    //             }
    //         })
    //     })
    // }
</script>
</body>
</html>